﻿using Microsoft.Extensions.Caching.Memory;
using OAS.Application.ReimburseApp.Dtos;
using OAS.Application.ReimburseApp.Services;
using OAS.Application.System.Dtos;
using OAS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OAS.Application.ReimburseApp
{
    public class ReimburseController : IDynamicApiController
    {
        private readonly ISqlSugarClient db;
        private readonly IReimburseService _reimburseService;
        public ReimburseController(IReimburseService reimburseService, ISqlSugarClient db)
        {
            this.db = db;
            _reimburseService = reimburseService;
        }
        public async Task<List<Reimburse>> GetAllReimburse()
        {
            return await _reimburseService.GetAllReimburse();
        }
        [HttpPost]
        public async Task<object> GetReimburseList(DtoQuery dto)
        {
            return await _reimburseService.GetReimburseList(dto);
        }
        public async Task AddReimburse(Reimburse reimburse)
        {
            await _reimburseService.AddReimburse(reimburse);
        }
        public async Task UpdateReimburse(Reimburse reimburse)
        {
            await _reimburseService.UpdateReimburse(reimburse);
        }
        public async Task DeleteReimburse([FromBody] string[] ids)
        {
            await _reimburseService.DeleteReimburse(ids);
        }
        public async Task AuditReimburse(DtoAudit dto)
        {
            await _reimburseService.AuditReimburse(dto);
        }
        [HttpPost]
        public async Task<string> UploadInvoice(IFormFile file)
        {
            // 如：保存到网站根目录下的 uploads 目录
            var savePath = Path.Combine(App.HostEnvironment.ContentRootPath, "uploads");
            if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);

            //// 这里还可以获取文件的信息
            // var size = file.Length / 1024.0;  // 文件大小 KB
            // var clientFileName = file.FileName; // 客户端上传的文件名
            // var contentType = file.ContentType; // 获取文件 ContentType 或解析 MIME 类型

            // 避免文件名重复，采用 GUID 生成
            var fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(savePath, fileName);

            // 保存到指定路径
            using (var stream = File.Create(filePath))
            {
                await file.CopyToAsync(stream);
            }

            // 返回文件名（这里可以自由返回更多信息）
            return fileName;
        }
        [HttpPost, NonUnify]
        public IActionResult DownloadInvoice(Reimburse reimburse)
        {
            string filePath = Path.Combine(App.HostEnvironment.ContentRootPath, "uploads/" + reimburse.Invoice);
            return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream")
            {
                FileDownloadName = reimburse.UserName + ((DateTime)reimburse.ReimburseTime).ToString("yyyyMMdd") + ".pdf" 
                // 配置文件下载显示名
            };
        }
    }
}
